package ${controllerPackage};

import ${poPackage}.${className};
import ${dtoPackage}.${className}Dto;
import ${servicePackage}.${className}Service;
import co.baiku.boot.common.message.Message;
import co.baiku.boot.common.message.PageData;
import co.baiku.boot.common.tools.ObjectTools;
import co.baiku.boot.common.validate.Validation;
import co.baiku.boot.common.validate.ValidationException;
import co.baiku.boot.core.orm.request.PagingRequest;
import co.baiku.boot.core.orm.service.BaseService;
import co.baiku.boot.core.web.controller.OrmController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.Logical;

/**
 * ${classComment}相关接口
 */
@RestController
<#if useTenantMode == true >@RequestMapping("/{tenantId}/${className}")</#if>
<#if useTenantMode == false >@RequestMapping("/${className}")</#if>
@Api(tags = {"${classComment}相关接口"})
public class ${className}Controller extends OrmController<${className}, ${className}Dto, ${pkType}> {
    @Resource
    private ${className}Service ${className?uncap_first}Service;

    /**
     * 新增 ${classComment}
     *
     * @param ${className?uncap_first}
     * @return
     */
    @PostMapping
    @ApiOperation("${classComment}-新增")
    <#if permissionsController == true >@RequiresPermissions(value = {"${className?uncap_first}Add", "${className?uncap_first}Edit"}, logical = Logical.OR)${'\n    '}</#if>public Message<${className}Dto> create(@RequestBody @ApiParam(name = "${classComment}对象", value = "传入JSON对象", required = true) ${className}Dto ${className?uncap_first}) {
        return Message.result(${className?uncap_first}Service.save${className}(${className?uncap_first}));
    }

    /**
    * 更新 ${classComment}
    *
    * @param ${className?uncap_first}
    * @return
    */
    @PutMapping("/{id}")
    @ApiOperation("${classComment}-更新")
    <#if permissionsController == true >@RequiresPermissions(value = {"${className?uncap_first}Add", "${className?uncap_first}Edit"}, logical = Logical.OR)${'\n    '}</#if>public Message<${className}Dto> update(@PathVariable @ApiParam(name = "id", value = "${classComment}的id字段", required = true, example = "1") Long id, @RequestBody @ApiParam(name = "${classComment}对象", value = "传入JSON对象", required = true) ${className}Dto ${className?uncap_first}) {
        ${className?uncap_first}.setId(id);
        return Message.result(${className?uncap_first}Service.save${className}(${className?uncap_first}));
    }

    /**
     * 分页查询${classComment}
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/page", produces = {"application/json;charset=UTF-8"})
    @ApiOperation(value = "${classComment}-分页列表", notes = "查询参数参考${className}Dto(返回结果)")
    <#if permissionsController  == true >@RequiresPermissions("${className?uncap_first}Read")${'\n    '}</#if>public Message<PageData<${className}Dto>> page(@RequestBody PagingRequest request) {
        return Message.result(${className?uncap_first}Service.findDtoPaging(request));
    }

    /**
     * 根据id返回对象
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "${classComment}-获取详情", notes = "")
    <#if permissionsController  == true >@RequiresPermissions("${className?uncap_first}Read")${'\n    '}</#if>public Message<${className}Dto> get(@PathVariable @ApiParam(name = "id", value = "${classComment}的id字段", required = true, example = "1") Long id) {
        return Message.result(${className?uncap_first}Service.getDtoById(id));
    }


    /**
     * 根据id列表批量删除对象
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    <#if permissionsController  == true >@RequiresPermissions("${className?uncap_first}Delete")${'\n    '}</#if>@ApiOperation(value = "${classComment}-批量删除", notes = "用于批量删除,也支持单个")
    public Message<String> delete(@RequestBody @ApiParam(name = "ids", value = "${classComment}的id列表", required = true, example = "[1,2,3]") List<${pkType}> ids) {
        ${className?uncap_first}Service.delete(ids);
        return Message.success("删除成功");
    }

    /**
     * 删除单个${classComment}
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "${classComment}-删除", notes = "不支持多个")
    <#if permissionsController  == true >@RequiresPermissions("${className?uncap_first}Delete")${'\n    '}</#if>public Message<String> delete(@PathVariable @ApiParam(name = "id", value = "${classComment}的id字段", required = true, example = "1") Long id) {
        ${className?uncap_first}Service.delete(id);
        return Message.success("删除成功");
    }

    @Override
    public BaseService<${className}, ${className}Dto, Long> getBaseService() {
        return ${className?uncap_first}Service;
    }
}
